﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Zadani09.db;
using Zadani09.db.ODS;

namespace Zadani09.umelec
{
    public partial class Umelci: System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ObjectDataSource2.Updated += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Updated);
            ObjectDataSource2.Inserted += new ObjectDataSourceStatusEventHandler(ObjectDataSource2_Inserted);
        }

        void ObjectDataSource2_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        void ObjectDataSource2_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            aktualizujData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void aktualizujData()
        {
            ObjectDataSource1.Select();
            GridView1.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            GridView1.DataBind();
            if (GridView1.Rows.Count <= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.Insert);
                schovejVyhledavani();
            }
            else if (DetailsView1.CurrentMode != DetailsViewMode.Edit &&
                     GridView1.SelectedIndex <= 0 &&
                     DetailsView1.DataItemIndex >= 0)
            {
                DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                GridView1.SelectedIndex = 0;
                ObjectDataSource2.Select();
                DetailsView1.DataBind();

                DetailsView1.PageIndex = 0;
                zobrazVyhledavani();
            }

            if (GridView1.Rows.Count > 0) //zapni vyhledavani vzdy kdyz je co hledat..
            {
                zobrazVyhledavani();
            }

            base.OnPreRender(e);
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            Session["vyhl_podle_admin_umelci"] = IsPostBack ? DropDownList1.SelectedValue : DropDownList1.Items[0].Value;

            base.OnPreRenderComplete(e);
        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            UmelecDataObject umelci = new UmelecDataObject();
            List<Umelec> vysl;
            List<string> vrat = new List<string>();

            vysl = umelci.cti_s_vyhledavanim(null, prefixText,
                        (string)HttpContext.Current.Session["vyhl_podle_admin_umelci"]);

            foreach (Umelec jeden in vysl)
                vrat.Add(jeden[HttpContext.Current.Session["vyhl_podle_admin_umelci"].ToString()].ToString());

            return (vrat.ToArray());
        }

        private void schovejVyhledavani()
        {
            UpdatePanel1.Visible = false;
            Button1.Visible = false;
            DropDownList1.Visible = false;
        }

        private void zobrazVyhledavani()
        {
            UpdatePanel1.Visible = true;
            Button1.Visible = true;
            DropDownList1.Visible = true;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox1.Text = ViewState["vyhl_admin_umelci"] == null ? "" : (string)ViewState["vyhl_admin_umelci"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ViewState["vyhl_admin_umelci"] = TextBox1.Text;
        }
    }
}
